package response;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/response_demo1")
public class response_demo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("demo1被访问了");
        /**
         * 访问demo1会自动跳转到demo2，实现重定向功能
         */
//        //1.设置状态码302
//        resp.setStatus(302);
//        //2.设置响应头location
//        resp.setHeader("location","/response_demo2");

        //3.更简单的办法
        /**
         * 当前的虚拟目录设置为/,如果以后修改虚拟目录，则路径会发生变化，
         * 所有可以使用getContextPath来获取虚拟目录后拼接
         */

        resp.sendRedirect(req.getContextPath()+"/response_demo2");
    }
}
